package com.itheima.sfbx.insurance.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.sfbx.framework.commons.utils.BeanConv;
import com.itheima.sfbx.insurance.dto.CoefficentVO;
import com.itheima.sfbx.insurance.mapper.CoefficentMapper;
import com.itheima.sfbx.insurance.pojo.CategoryCoefficent;
import com.itheima.sfbx.insurance.pojo.Coefficent;
import com.itheima.sfbx.insurance.pojo.InsuranceCoefficent;
import com.itheima.sfbx.insurance.service.ICategoryCoefficentService;
import com.itheima.sfbx.insurance.service.ICoefficentService;
import com.itheima.sfbx.insurance.service.IInsuranceCoefficentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
/**
 * @Description：系数项服务实现类
 */
@Slf4j
@Service
public class CoefficentServiceImpl extends ServiceImpl<CoefficentMapper, Coefficent> implements ICoefficentService {



    @Override
    public Page<CoefficentVO> findPage(CoefficentVO coefficentVO, int pageNum, int pageSize) {
        Page<Coefficent> page = this.lambdaQuery()
                .eq(StrUtil.isNotBlank(coefficentVO.getCoefficentKey()), Coefficent::getCoefficentKey, coefficentVO.getCoefficentKey())
                .like(StrUtil.isNotBlank(coefficentVO.getCoefficentKeyName()), Coefficent::getCoefficentKeyName, coefficentVO.getCoefficentKeyName())
                .eq(StrUtil.isNotBlank(coefficentVO.getDataState()), Coefficent::getDataState, coefficentVO.getDataState())
                .page(new Page<>(pageNum, pageSize));
        if (ObjectUtil.isEmpty(page)){
            throw new RuntimeException("数据不存在");
        }
        return BeanConv.toPage(page, CoefficentVO.class);
    }

    @Override
    public CoefficentVO findById(String coefficentId) {
        Coefficent coefficent = this.getById(coefficentId);
        if (ObjectUtil.isEmpty(coefficent)){
            throw new RuntimeException("系数想不存在");
        }
        return BeanConv.toBean(coefficent, CoefficentVO.class);
    }

    @Autowired
    private IInsuranceCoefficentService insuranceCoefficentService;

    @Autowired
    private ICategoryCoefficentService categoryCoefficentService;

    @Override
    @Transactional
    public CoefficentVO save(CoefficentVO coefficentVO) {
        String coefficentKey = coefficentVO.getCoefficentKey();
        Integer count = this.lambdaQuery().eq(Coefficent::getCoefficentKey, coefficentKey).count();
        if (count>0){
            throw new RuntimeException("系数项存在，不能重复添加");
        }
        Coefficent bean = BeanConv.toBean(coefficentVO, Coefficent.class);
        boolean save = this.save(bean);
        if (!save){
            throw new RuntimeException("操作失败");
        }
        return BeanConv.toBean(bean, CoefficentVO.class);
    }

    @Override
    @Transactional
    public Boolean update(CoefficentVO coefficentVO) {
        Long id = coefficentVO.getId();
        Integer count = this.lambdaQuery().eq(Coefficent::getId, id).count();
        if (count==0 || id==null){
            throw new RuntimeException("系数项不存在，无法修改");
        }
        Integer count1 = insuranceCoefficentService.lambdaQuery()
                .eq(InsuranceCoefficent::getCoefficentKey, coefficentVO.getCoefficentKey())
                .count();
        Integer count2 = categoryCoefficentService.lambdaQuery()
                .eq(CategoryCoefficent::getCoefficentKey, coefficentVO.getCoefficentKey())
                .count();
        if (count1>0 || count2>0){
            throw new RuntimeException("系数项存在绑定关系，无法修改");
        }
        Coefficent bean = BeanConv.toBean(coefficentVO, Coefficent.class);
        return this.updateById(bean);
    }

    @Override
    @Transactional
    public Boolean delete(String[] checkedIds) {
        List<String> idList=new ArrayList<>();
        for (String checkedId : checkedIds) {
            Coefficent coefficent = this.getById(checkedId);
            Integer count1 = insuranceCoefficentService.lambdaQuery()
                    .eq(InsuranceCoefficent::getCoefficentKey, coefficent.getCoefficentKey())
                    .count();
            Integer count2 = categoryCoefficentService.lambdaQuery()
                    .eq(CategoryCoefficent::getCoefficentKey, coefficent.getCoefficentKey())
                    .count();
            if (count1>0 || count2>0){
                throw new RuntimeException("系数存在绑定关系，无法删除");
            }
            idList.add(checkedId);
        }
        if (CollUtil.isEmpty(idList)){
            throw new RuntimeException("集合为空");
        }
        return this.removeByIds(idList);
    }

    @Override
    public List<CoefficentVO> findList(CoefficentVO coefficentVO) {
        //TODO
        return null;
    }

    @Override
    public CoefficentVO findByCoefficentKey(String coefficentKey) {
        //TODO
        return null;
    }
}
